var JargonCommon = {
	isInitialized : false,
	isEnabled: true,
	isLogEnabled : true,
	defaultJargon : null,
	siteJargons : null,
	
	Constants : {
		keyboardDelete : 46
	},
	
	Preferences : {
		_manager : null,
		_prefix : 'extensions.jargon.',
		defaultJargon : 'defaultJargon',
		siteJargons : 'siteJargons',
		isEnabled : 'isEnabled',
		
		init : function()
		{
			this._manager = Components.classes['@mozilla.org/preferences-service;1']
								.getService(Components.interfaces.nsIPrefBranch);
		},
		
		getStringPref : function(key)
		{
			try 
			{
				JargonCommon.Logger.log('getStringPref(\'' + key + '\')');
				return this._manager.getCharPref(this._prefix + key);
			}
			catch (ex)
			{
				return '';
			}
		},
		
		setStringPref : function(key, value)
		{
			try 
			{
				JargonCommon.Logger.log('setStringPref(\'' + key + '\'' + ',\'' + value + '\')');
				return this._manager.setCharPref(this._prefix + key, value);
			}
			catch (ex)
			{
				JargonCommon.displayMessage(ex);
				return null;
			}
		},
		
		getBoolPref : function(key)
		{
			try 
			{
				JargonCommon.Logger.log('getBoolPref(\'' + key + '\')');
				return this._manager.getBoolPref(this._prefix + key);
			}
			catch (ex)
			{
				return false;
			}
		},
		
		setBoolPref : function(key, value)
		{
			try 
			{
				JargonCommon.Logger.log('setBoolPref(\'' + key + '\'' + ',' + value + ')');
				return this._manager.setBoolPref(this._prefix + key, value);
			}
			catch (ex)
			{
				JargonCommon.displayMessage(ex);
				return null;
			}
		}
	},
	
	Logger : {
		console : Components.classes['@mozilla.org/consoleservice;1']
					.getService(Components.interfaces.nsIConsoleService),
		
		log : function(message)
		{
			if (JargonCommon.isLogEnabled)
			{
				this.console.logStringMessage('[jargon]' + new Date() + '\n' + message + '\n');
			}
		}
	},
		
	convertListBoxToString : function(listbox)
	{
		var str = '';
		var listitem = null;
		var totalRows = listbox.getRowCount();
		for (var i = 0; listitem = listbox.getItemAtIndex(i), i < totalRows; i++)
		{
			if (!listitem.hasChildNodes())
			{
				str += listitem.getAttribute('label');
			}
			else
			{
				var listcell = null;
				for (var j = 0; listcell = listitem.childNodes[j], !!listcell; j++)
				{
					str += listcell.getAttribute('label') + ',';
				}
				str = str.substring(0, str.length - 1); // remove the last comma (,)
			}
			str += ';';	// separate rows with a semicolon
		}
		str = str.substring(0, str.length - 1);	// remove the last semicolon
		
		return str;
	},
	
	areSitesEqual : function(to, against)
	{
		return to.toLowerCase() == against.toLowerCase();
	},
	
	displayMessage : function(message)
	{
		alert(message);
	},
	
	prompt : function(message, defaultInput)
	{
		return prompt(message, defaultInput);
	},
	
	loadPreferences : function()
	{
		this.Logger.log('loading preferences...');
		this.defaultJargon = this.Preferences.getStringPref(this.Preferences.defaultJargon);
		this.Logger.log('default jargon = ' + this.defaultJargon);
		
		this.siteJargons = this.Preferences.getStringPref(this.Preferences.siteJargons);
		this.Logger.log('site jargons = ' + this.siteJargons);
		
		this.isEnabled = this.Preferences.getBoolPref(this.Preferences.isEnabled);
		this.Logger.log('is enabled = ' + this.isEnabled);
	},
	
	init : function()
	{
		if (!this.isInitialized)
		{
			this.Logger.log('initializing..');
			this.Preferences.init();
			this.loadPreferences();
			
			this.isInitialized = true;
		}
	}
}

JargonCommon.init();